//
//  RSService.swift
//  ProjectApp
//
//  Created by jack on 2021/1/12.
//

import Foundation


/// RX 接口请求, 使用RXSwift对Service接口再次封装
/// - Parameters:
///   - params:
///   - errorToast:
///   - hud:
/// - Returns:
func rxrequest<T: Codable>(params: RequestParams, errorToast: Bool, hud: Bool) -> Observable<RespEntity<T>> {
    let observable:Observable<RespEntity<T>> = Observable.create { (observer) -> Disposable in
        request(params: params, errorToast: errorToast, hud: hud) { (suc: Bool, resp: RespEntity<T>?, error: Error?) in
            if let model = resp {// 返回有正确模型则说明接口调用正确
                observer.on(.next(model))
            } else {// 网络出错、接口数据模型不对
                if let e = error {
                    observer.onError(e)
                } else {
                    let e = SError.desc("no error")
                    observer.onError(e)
                }
            }
            observer.onCompleted()
        }
        return Disposables.create {
          
        }
    }
    return observable
}

enum SError: Error {
    case desc(String)
}
